class Solution {
    public:
        vector<vector<int>> generateMatrix(int n) {
            vector<vector<int>> v(n,vector<int>(n,0));
            int sx=0,sy=0;
            int loop=n/2;
            int i,j;
            int offset=1;
            int count=1;
            while(loop--){
                i=sx;
                j=sy;
                for(j;j<n-offset;j++){//左闭右开
                    v[i][j]=count++;
                }
                for(i;i<n-offset;i++){//左闭右开
                    v[i][j]=count++;  
                }
                for(j;j>sy;j--){//左开右闭
                    v[i][j]=count++;  
                }
                for(i;i>sx;i--){//左闭右开
                    v[i][j]=count++;    
                }
                sx++;
                sy++;
                offset++;
            }
            if(n%2==1){
                v[n/2][n/2]=count;
            }
            return v;
        }
    };